<?php

/**
 * @author gbfun <gbfun@163.com>  2018-06-20 16:28
 * http://www.wuweiyou.com
 */

class db_model_order_good
{
    protected $_db_table;   
    protected $_formatter;
    
    protected static $_instance;
    
    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();    //by gbfun 又是静态变量
        }
    
        return self::$_instance;
    }
    
    private function __clone()
    {
    }
    
    protected function __construct()
    {
    }

    public function get_db_table()
    {
        if (null === $this->_db_table) {
            $this->_db_table = db_table_order_good::get_instance();
        }
        
        return $this->_db_table;
    }
    
    public function get_formatter()
    {
        if (null === $this->_formatter) {
            $this->_formatter = formatter_order_good::get_instance();
        }
    
        return $this->_formatter;
    }
    
    public function set_rows(& $rows)
    {
        foreach ($rows as &$row) {
            $this->set_row($row);            
        }
    }
    
    public function set_row(& $row)
    {
//         $formatter = $this->get_formatter();
        
//         $formatter->set_by_create_time($row);
//         $formatter->set_by_update_time($row);
//         $formatter->set_by_status($row);
    }
    
    public function set_rows_user(& $rows)
    {
        foreach ($rows as &$row) {
            $this->set_row_user($row);
        }
    }
    
    public function set_row_user(& $row)
    {
//         $this->get_formatter()->set_by_create_time($row);
//         $this->get_formatter()->set_by_update_time($row);
    }
    
    public function find($id)
    {
        $row = $this->get_db_table()->find($id);
        //var_dump($row); exit();
    
        if(!empty($row)){
            $this->set_row($row);
            //var_dump($row); exit();
        }
    
        return $row;
    }
    
    public function fetch_row($where)
    {
        $row = $this->get_db_table()->fetch_row($where);
        //var_dump($row); exit();
    
        if(!empty($row)){
            $this->set_row($row);
            //var_dump($row); exit();
        }
    
        return $row;
    }
    
    public function fetch_all($where = null, $order = null)
    {
        $rows = $this->get_db_table()->fetch_all($where, $order);
        //var_dump($rows); exit();
    
        $this->set_rows($rows);
        //var_dump($rows); exit();
    
        return $rows;
    }
    
    public function get_list($where = null, $order = null, $options = array())
    {
        $list = $this->get_db_table()->get_list($where, $order, $options);
        //var_dump($list); exit();
    
//         $rows = $list['list'];
//         $this->set_rows($rows);
//         $list['list'] = $rows;
        $this->set_rows($list['list']);
        //var_dump($list); exit();
    
        return $list;
    }
    
    public function get_simple_list($where = null, $order = null, $options = array())
    {
    
    }    
    
    public function  add_order_good($order_info)
    {
        $order_good_id_list = array();
        
        $order_id  = $order_info['order_id'];
        $cart_list = $order_info['cart_list_info']['cart_list'];
    
        foreach($cart_list as $item){    //@by gbfun 
            $order_good_options = array(
                'order_id'         => $order_id,
                'good_id'          => $item['good_id'],
                'spec_id'          => $item['spec_id'],
                'good_name'        => $item['good_name'],
                'spec_desc'        => $item['spec_desc'],
                
                'good_price'       => $item['good_price'],
                'good_number'      => $item['good_number'],
                'good_thumb'       => $item['good_thumb'],
                'item_total_price' => $item['total'],
            );
             
            $order_good_id = $this->get_db_table()->add($order_good_options);
            //var_dump($order_good_id); exit();
            $order_good_id_list[] = $order_good_id;
        }
        
        return $order_good_id_list;
    }
}

